package com.traveldream.beans.utility;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import com.traveldream.entities.User;
import com.traveldream.entity_managers.CustomerManager;
import com.traveldream.entity_managers.ProductManager;

@ManagedBean(name="authentication")
@SessionScoped
public class AuthenticationBean {

	
	private User user;
	
	@EJB
	private CustomerManager cm;
	
	
	public AuthenticationBean(){
		user = new User();
	}
	
	public String logoutAction(){
		user = new User(); // reset current user
		FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
		return "/index?faces-redirect=true";
	}

	public String registrationAction(){
		System.out.print("new registered user");
		if(cm.create(this.user)!=null){
			this.user = new User();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Registrazione completata correttamente!"));
			return "/auth/login";
		}
		else
		{
			this.user.setEmail("");
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Impossibile completare la registrazione, l'indirizzo email inserito esiste gia'!"));
			return "/auth/registration";
		}
		
	}
	
	
	public User getAuthenticatedUser()
	{
		return cm.getAuthenticatedUser();
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	

}
